package com.sxt.business.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.business.domain.Goods;
import com.sxt.business.domain.Inport;
import com.sxt.business.domain.Provider;
import com.sxt.business.mapper.InportMapper;
import com.sxt.business.service.IGoodsService;
import com.sxt.business.service.IInportService;
import com.sxt.business.service.IProviderService;
import com.sxt.business.vo.InportVO;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.WebAppUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-15
 */
@Service
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements IInportService {

	/*
	 * @Autowired
	 * 
	 * @Lazy private IProviderService providerService;
	 * 
	 * @Autowired
	 * 
	 * @Lazy private IGoodsService goodsService;
	 */
	
	@Override
	public DataGridView queryAllInport(InportVO inportVO) {
		IPage<Inport> page = new Page<>(inportVO.getPage(), inportVO.getLimit());  
		
		QueryWrapper<Inport> qw = new QueryWrapper<Inport>();
		qw.eq(inportVO.getProviderid()!=null, "providerid", inportVO.getProviderid());
		qw.eq(inportVO.getGoodsid()!=null, "goodsid", inportVO.getGoodsid());
		qw.ge(inportVO.getStartTime() != null, "inporttime", inportVO.getStartTime());
		qw.le(inportVO.getEndTime() != null, "inporttime", inportVO.getEndTime());
		qw.orderByDesc("inporttime");	
		this.getBaseMapper().selectPage(page, qw);
		List<Inport> list = page.getRecords();
		for (Inport inport : list) {
			Integer providerid = inport.getProviderid();
			if(null!=providerid) {
				Provider provider = WebAppUtils.getContent().getBean(IProviderService.class).getById(providerid);
				inport.setProvidername(provider.getProvidername());
			}
			
			Integer goodsid = inport.getGoodsid();
			if(null!=goodsid) {
				Goods goods = WebAppUtils.getContent().getBean(IGoodsService.class).getById(goodsid);
				inport.setGoodsname(goods.getGoodsname());
				inport.setSize(goods.getSize());
			}
		}
		return new DataGridView(page.getTotal(),list);
	}

	@Override
	public Inport addInport(Inport inport) {
		this.getBaseMapper().insert(inport);
		//增加商品库存
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(inport.getGoodsid());
		goods.setNumber(goods.getNumber()+inport.getNumber());
		iGoodsService.updateById(goods);
		return inport;
	}

	@Override
	public Inport updateInport(Inport inport) {
		//查询旧的进货单
		Inport oldInport = this.getBaseMapper().selectById(inport.getId());
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(inport.getGoodsid());
		//总库存-旧订单的库存+新修改之后的数量
		goods.setNumber(goods.getNumber()-oldInport.getNumber()+inport.getNumber());
		iGoodsService.updateById(goods);
		this.getBaseMapper().updateById(inport);
		return inport;
	}

	@Override
	public boolean removeById(Serializable id) {
		Inport inport = this.getBaseMapper().selectById(id);
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(inport.getGoodsid());
		goods.setNumber(goods.getNumber()-inport.getNumber());
		iGoodsService.updateById(goods);
		return super.removeById(id);
	}
	
	

}
